<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>抖动</title>
<style type="text/css">
img{position: absolute; top:200px;left: 400px;}
</style>
<script type="text/javascript">
window.onload = function(){
	var oImg = document.getElementById('img');
	var flag = true;
	oImg.onclick = function(){
		if (!flag) return;
		flag = false;
		shake(oImg,'left',function(){
			shake(oImg,'top',function () {
				flag = true;
			});
		});
		
	}
}
	function getStyle(obj,attr){
		return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
	}

	function shake(obj,attr,endFn){
		var arr = [];
		var num = 0;
		var pos = parseInt(getStyle(obj,attr));

		for (var i=20;i>0;i-=2){
			arr.push(i,-i)
		}
		arr.push(0);

		clearInterval(obj.timer);
		obj.timer = setInterval(function(){
			obj.style[attr] = pos + arr[num] + 'px';
			num ++;
			if (num === arr.length) {
				clearInterval(obj.timer);
				endFn && endFn();
			};
		},50)	
	}
</script>
</head>
<body>
<img  id="img" src="img/opera1.jpg">
</body>
</html>